<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="门店" prop="deptId">
        <el-select v-model="queryParams.deptId" placeholder="请选择门店">
          <el-option
            v-for="item in slectDeptOptions"
            :key="item.deptId"
            :label="item.deptName"
            :value="item.deptId">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="销售员">
        <el-select v-model="queryParams.userId" placeholder="请选择销售员">
            <el-option
              v-for="item in slectUserOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
      </el-form-item>

      <el-form-item label="班次" prop="classes">
        <el-select v-model="queryParams.classes" placeholder="请选择班次" clearable>
          <el-option
            v-for="dict in dict.type.class"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="盘点日期">
        <el-date-picker
          v-model="daterangeCheckTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:checkFund:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:checkFund:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:checkFund:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:checkFund:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" show-summary :summary-method="getSummaries" :data="checkFundList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <!-- <el-table-column label="门店id" align="center" prop="deptId" /> -->
      <el-table-column label="门店" align="center" prop="deptName" />
      <el-table-column label="班次" align="center" prop="classes">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.class" :value="scope.row.classes"/>
        </template>
      </el-table-column>
      <el-table-column label="销售员" align="center" prop="sellName" />
      <el-table-column label="现金" align="center" prop="xj" />
      <el-table-column label="微信余额" align="center" prop="wxBalance" />
      <el-table-column label="收银通" align="center" prop="hsk" />
      <el-table-column label="盘点日期" align="center" prop="checkTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:checkFund:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:checkFund:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改盘点--资金盘点对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="门店" prop="deptId">
          <el-select v-model="form.deptId" placeholder="请选择门店">
            <el-option
              v-for="item in slectDeptOptions"
              :key="item.deptId"
              :label="item.deptName"
              :value="item.deptId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="销售员" prop="sellId">
          <el-select v-model="form.sellId" placeholder="请选择销售员">
            <el-option
              v-for="item in slectUserOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="现金" prop="xj">
          <el-input v-model="form.xj"  type="number" placeholder="请输入现金" />
        </el-form-item>
        <el-form-item label="微信余额" prop="wxBalance">
          <el-input v-model="form.wxBalance"  type="number" placeholder="请输入微信余额" />
        </el-form-item>
        <el-form-item label="收银通" prop="hsk">
          <el-input v-model="form.hsk"  type="number" placeholder="请输入收银通" />
        </el-form-item>
<!--        <el-form-item label="支付宝" prop="zfb">-->
<!--          <el-input v-model="form.zfb"  type="number" placeholder="请输入支付宝" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="数字人民币" prop="szRmb">-->
<!--          <el-input v-model="form.szRmb" type="number" placeholder="请输入数字人民币" />-->
<!--        </el-form-item>-->
        <el-form-item label="班次" prop="classes">
          <el-radio-group v-model="form.classes">
            <el-radio
              v-for="dict in dict.type.class"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="盘点日期" prop="checkTime">
          <el-date-picker clearable
            v-model="form.checkTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择盘点日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCheckFund, getCheckFund, delCheckFund, addCheckFund, updateCheckFund } from "@/api/system/checkFund";
import { listDept } from "@/api/system/dept";
import { getSalesUserByUserName } from "@/api/system/user";
export default {
  name: "CheckFund",
  dicts: ['class'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 盘点--资金盘点表格数据
      checkFundList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 删除标志时间范围
      daterangeCheckTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        xj: null,
        hsk: null,
        zfb: null,
        szRmb: null,
        classes: null,
        checkTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      slectDeptOptions:[],
      slectUserOptions: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询盘点--资金盘点列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCheckTime && '' != this.daterangeCheckTime) {
        this.queryParams.params["beginCheckTime"] = this.daterangeCheckTime[0];
        this.queryParams.params["endCheckTime"] = this.daterangeCheckTime[1];
      }
      listCheckFund(this.queryParams).then(response => {
        this.checkFundList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listDept({"parentId":100}).then(response => {
        this.slectDeptOptions = response.data;
      });
      getSalesUserByUserName({"userName":""}).then(response => {
        this.slectUserOptions = response.data;
      });
    },
  getSummaries(param){
    const { columns, data } = param
    const sums = [];
    columns.forEach((column, index) => {
      //第一个显示为合计
      if (index === 0) {
        sums[index] = '合计'
        return
      }
      const values = data.map((item) => item[column.property])
      if(index >=4 && index <=8) {
        //对表格数据进行循环
        values.map((item) => {
          //判断返回的是否为number数据类型
          if (typeof item === 'number' && item !== Infinity && !isNaN(item)) {
            //判断是否为时间戳
            if (item > 160000000000) {
              //若为时间戳则置为--
              sums[index] = '-'
              return
            } else {
              //对number数据进行累加
              sums[index] = values.reduce((prev, curr) => {
                const value = Number(curr)
                if (!isNaN(value)) {
                  //解决js计算小数出现小数点后多位数字的问题
                  return Math.floor((prev + curr) * 100) / 100
                } else {
                  return prev
                }
              }, 0)
            }
          } else {
            //不为number类型则置为--
            sums[index] = '-'
          }
        })
      }

    })
    return sums
  },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        xj: null,
        hsk: null,
        zfb: null,
        szRmb: null,
        classes: null,
        checkTime: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCheckTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加盘点--资金盘点";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCheckFund(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改盘点--资金盘点";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCheckFund(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCheckFund(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除盘点--资金盘点编号为"' + ids + '"的数据项？').then(function() {
        return delCheckFund(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/checkFund/export', {
        ...this.queryParams
      }, `checkFund_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
